﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using MySql.Data.MySqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TJ_MainNameSpace
{
    public partial class Form_k15 : Form
    {
        private kongjian15.sanxiangdianya mydianyahuganqi;
        public Form_k15(kongjian15.sanxiangdianya id)
        {
            InitializeComponent();
            this.mydianyahuganqi = id;
        }
        private bool loading = false;
        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);

        private void Form1_Load(object sender, EventArgs e)
        {
            loading = true;
            this.textBox1.Text = mydianyahuganqi.Name;
            this.textBox3.Text = mydianyahuganqi.Dianya_Ua;
            this.textBox4.Text = mydianyahuganqi.Dianya_Ub;
            this.textBox5.Text = mydianyahuganqi.Dianya_Uc;
            this.textBox6.Text = mydianyahuganqi.FaultSignal;
            try
            {
                con.Open();
                string sql1 = "select distinct db_areaname from dbpoint where project = '" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";

                MySqlDataAdapter oda1 = new MySqlDataAdapter(sql1, con);

                DataSet ds3 = new DataSet();

                oda1.Fill(ds3, "dbpoint");

                this.comboBox1.DataSource = ds3.Tables[0];
                this.comboBox1.DisplayMember = "db_areaname";
                con.Close();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }

            loading = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            string mysql = "SELECT  ctr_name  FROM  run_ctr where ctr_name='" + mydianyahuganqi.Name + "'";

            MySqlCommand cmd = new MySqlCommand(mysql, con);
            object count = cmd.ExecuteScalar();

            string sql;

            if (count == null)

                sql = "INSERT INTO run_ctr(faultname,ctr_name,ctr_kind,project) VALUES('" + comboBox3.Text + "', '" + textBox1.Text + "' ,'K15','" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "')";

            else
                sql = "update run_ctr set ctr_name='" + textBox1.Text + "',faultname='" + comboBox3.Text + "' where ctr_name='" + mydianyahuganqi.Name + "'";

            MySqlCommand mInsertCmd = new MySqlCommand(sql, con);
            mInsertCmd.CommandType = CommandType.Text;
            mInsertCmd.ExecuteNonQuery();
            con.Close();

            mydianyahuganqi.Name = this.textBox1.Text;
            mydianyahuganqi.FaultSignal = this.textBox6.Text;
            mydianyahuganqi.Dianya_Ua = this.textBox3.Text;
            mydianyahuganqi.Dianya_Ub = this.textBox4.Text;
            mydianyahuganqi.Dianya_Uc = this.textBox5.Text;
            this.mydianyahuganqi.Refresh();
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loading) return;
            fresh();
        }

        private void fresh()
        {
            con.Open();
            string sql = "select * from dbpoint where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'AND db_areaname='" + comboBox1.Text + "'";
            MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);

            DataSet ds = new DataSet();
            ds.Clear();
            oda.Fill(ds, "dbpoint");
            this.comboBox2.DataSource = ds.Tables["dbpoint"];
            this.comboBox2.DisplayMember = "db_name";
            this.comboBox3.DataSource = ds.Tables["dbpoint"];
            this.comboBox3.DisplayMember = "db_name";
            this.comboBox4.DataSource = ds.Tables["dbpoint"];
            this.comboBox4.DisplayMember = "db_name";
            this.comboBox5.DataSource = ds.Tables["dbpoint"];
            this.comboBox5.DisplayMember = "db_name";
            con.Close();
        }

    }
}